﻿using System;
using System.Xml.Linq;
using AuthoritExtensibility;
using API = Authorit.API;
using Dto = Authorit.API.Dto;

namespace MiniImporter {

  class Importer {

    static void Main(string[] args) {

      API.Configuration.LibraryConfigurationList config = null;
      API.Services.Authentication authentication = null;
      API.ApplicationContext context = null;
      API.Services.Topic topicservice = null;
      Dto.Topic topicDto = new Dto.Topic();

      string server = @""; // Supply correct address for your server
      string alias = "Library";
      string database = ""; // Supply correct name for database, e.g., "production"
      int folder = 0; // Supply correct folder id
      string user = "", pass = ""; // Supply correct username and password
      string description = "New Topic", content = "Here is some content. Isn't it wonderful?";

      config = new API.Configuration.LibraryConfigurationList(new API.Configuration.SqlServerLibraryConfiguration(alias, server, database));
      //config = new API.Configuration.LibraryConfigurationList(new API.Configuration.JetLibraryConfiguration(alias, server));
      context = new API.ApplicationContext(config);
      authentication = context.GetServiceInstance<API.Services.Authentication>();
      topicservice = context.GetServiceInstance<API.Services.Topic>();

      Dto.LoginResponse response = new Dto.LoginResponse();
      response = authentication.Login(alias, user, pass);

      XElement topic = new XElement("Text");
      topic.Add(new XElement("p", new XAttribute("id", 4), content));

      topicDto.Description = description;
      topicDto.HelpHeading = description;
      topicDto.PrintHeading = description;
      topicDto.WebHeading = description;
      topicDto.FolderId = folder;
      topicDto = topicservice.Create(topicDto);
      topicservice.SaveTopicTextFromXml(topicDto.ObjectId, topic.ToString());
      
      Console.WriteLine("Done.");
      
      authentication.Logout();
      
    }
  }
}
